/*     */ package com.zimbra.cs.mailclient;
/*     */ 
/*     */ import com.zimbra.common.util.Log;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.EOFException;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class MailInputStream
/*     */   extends InputStream
/*     */ {
/*     */   private Log log;
/*     */   private ByteArrayOutputStream logbuf;
/*     */   protected final InputStream in;
/*     */   protected final StringBuilder sbuf;
/*  40 */   private int nextByte = -1;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public MailInputStream(InputStream is)
/*     */   {
/*  48 */     this.in = is;
/*  49 */     this.sbuf = new StringBuilder(132);
/*     */   }
/*     */   
/*     */   public MailInputStream(InputStream is, Log log) {
/*  53 */     this(is);
/*  54 */     this.log = log;
/*  55 */     this.logbuf = new ByteArrayOutputStream(1024);
/*     */   }
/*     */   
/*     */   public int read(byte[] b, int off, int len) throws IOException
/*     */   {
/*  60 */     if (b == null)
/*  61 */       throw new NullPointerException();
/*  62 */     if ((off < 0) || (off > b.length) || (len < 0) || (off + len > b.length) || (off + len < 0))
/*     */     {
/*  64 */       throw new IndexOutOfBoundsException(); }
/*  65 */     if (len == 0) {
/*  66 */       return 0;
/*     */     }
/*  68 */     if (this.nextByte != -1) {
/*  69 */       b[(off++)] = ((byte)this.nextByte);
/*  70 */       this.nextByte = -1;
/*  71 */       len = this.in.read(b, off, len - 1);
/*  72 */       if ((this.logbuf != null) && (len > 0)) {
/*  73 */         this.logbuf.write(b, off, len);
/*     */       }
/*  75 */       return len != -1 ? len + 1 : 1;
/*     */     }
/*  77 */     len = this.in.read(b, off, len);
/*  78 */     if ((this.logbuf != null) && (len > 0)) {
/*  79 */       this.logbuf.write(b, off, len);
/*     */     }
/*  81 */     return len;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public String readLine()
/*     */     throws IOException
/*     */   {
/*  92 */     this.sbuf.setLength(0);
/*  93 */     int c = read();
/*  94 */     if (c == -1) return null;
/*  95 */     while ((c != 10) && (c != -1)) {
/*  96 */       this.sbuf.append((char)c);
/*  97 */       c = read();
/*     */     }
/*  99 */     int len = this.sbuf.length();
/* 100 */     if ((len > 0) && (this.sbuf.charAt(len - 1) == '\r')) {
/* 101 */       this.sbuf.setLength(len - 1);
/*     */     }
/* 103 */     return this.sbuf.toString();
/*     */   }
/*     */   
/*     */   public int read() throws IOException
/*     */   {
/* 108 */     if (this.nextByte != -1) {
/* 109 */       int b = this.nextByte;
/* 110 */       this.nextByte = -1;
/* 111 */       if ((this.logbuf != null) && (b >= 0)) {
/* 112 */         this.logbuf.write(b);
/*     */       }
/* 114 */       return b;
/*     */     }
/* 116 */     int b = this.in.read();
/* 117 */     if ((this.logbuf != null) && (b >= 0)) {
/* 118 */       this.logbuf.write(b);
/*     */     }
/* 120 */     return b;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public int peek()
/*     */     throws IOException
/*     */   {
/* 133 */     if (this.nextByte == -1) {
/* 134 */       this.nextByte = this.in.read();
/*     */     }
/* 136 */     return this.nextByte;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public char readChar()
/*     */     throws IOException
/*     */   {
/* 147 */     int c = read();
/* 148 */     if (c == -1) throw new EOFException("Unexpected end of stream");
/* 149 */     return (char)c;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public char peekChar()
/*     */     throws IOException
/*     */   {
/* 162 */     int c = peek();
/* 163 */     if (c == -1) throw new EOFException("Unexpected end of stream");
/* 164 */     return (char)c;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public boolean isEOF()
/*     */     throws IOException
/*     */   {
/* 174 */     return peek() == -1;
/*     */   }
/*     */   
/*     */   public long skip(long n) throws IOException
/*     */   {
/* 179 */     if (n <= 0L) {
/* 180 */       return 0L;
/*     */     }
/* 182 */     if (this.nextByte != -1) {
/* 183 */       this.nextByte = -1;
/* 184 */       return this.in.skip(n - 1L) + 1L;
/*     */     }
/* 186 */     return this.in.skip(n);
/*     */   }
/*     */   
/*     */   public void close()
/*     */     throws IOException
/*     */   {
/* 192 */     this.in.close();
/*     */   }
/*     */   
/*     */   public final void trace() {
/* 196 */     if ((this.logbuf == null) || (this.logbuf.size() == 0)) {
/* 197 */       return;
/*     */     }
/* 199 */     if (this.log.isTraceEnabled()) {
/* 200 */       this.log.trace("S: %s", new Object[] { this.logbuf.toString().trim() });
/*     */     }
/* 202 */     this.logbuf.reset();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailclient/MailInputStream.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */